<?php 
require_once('model.php');

class usermodel extends model{ 
  // private $connection;
   function __construct(){
   	parent::__construct();
   }
	function getuserlist(){
	/*$data = array(
	array('username'=>'jason','gender'=>'男'),
	array('username'=>'lala','gender'=>'男'),
	array('username'=>'rose','gender'=>'女')
	);
*/
	$sql = 'select * from users';
	$data = $this->connection->query($sql);
	return $data;
}
	function adduser($data){
		
		if(!empty($_FILES["pic"]["name"])){
		  	$pic = $this->uploadfile('pic','images/',array('jpg','gif','png'));
		}
		else{
		  	$pic='';
		}
		$user = array(
			'uid'=>'',
			'uname'=>$data['uname'],
			'password'=>$data['password'],
			'gender'=>$data['gender'],
			'birthday'=>$data['birthdate'],
			'hobby'=>implode($data['hobby'],','),
			'degree'=>$data['degree'],
			'intro'=>$data['intro'],
			'pic'=>$pic
			);
		var_dump($user);
		$sql = "insert into users(uid,uname,password,gender,birthdate,hobby,degree,intro,pic) 
			values(null,'".$user['uname']."','".$user['password']."','"
			.$user['gender']."','".$user['birthday']."','".$user['hobby']
			."','".$user['degree']."','".$user['intro']."','".$pic."') ";
		
		$data = $this->connection->query($sql);
		return $data;
	}
	function getuserbyid($id){
		//echo $id;
		$sql = 'select * from users where uid='.$id.' ';
		$data = $this->connection->query($sql);
		return $data;

	}
	function updateuser($data){
		//var_dump($data);
		if(!empty($_FILES["pic"]["name"])){
		  	$pic = $this->uploadfile('pic','images/',array('jpg','gif','png'));
		}
		else{
		  	$pic='';
		}
		//echo $_FILES["pic"]["name"];
		$user = array(
			'uid'=>$data['id'],
			'uname'=>$data['uname'],
			'gender'=>$data['gender'],
			'birthdate'=>$data['birthdate'],
			'hobby'=>implode(",",$data['hobby']),
			'degree'=>$data['degree'],
			'intro'=>$data['intro'],
			'pic'=>$pic
			);
		$sets = '';
		foreach($user as $key =>$val){
			$sets .=  $key."='".$val."',";
		}
		$sets = rtrim($sets,',');
		
	    $sql='UPDATE users SET '.$sets.' where uid='.$data['id'].' ';  
	   // var_dump($user);
	   // echo $sql;
	    
	    $res=mysql_query($sql);

	    if ($res){
	        return mysql_affected_rows();
	    }else {
	        return false;
	    }
    }
    function deluser($id){
    	//echo $id;
    	$sql = "delete from users where uid=".$id." ";
    	//echo $sql;
    	$data = $this->connection->query($sql);
    	return $data;
    }

}



 ?>